/**
 * author: William Cheng
 * July 3rd, 2012
*/
window.onload = function(){
    try{
	    // creating canvas objects
	    var canvas = document.getElementById("myCanvas");
	    var ctx = canvas.getContext('2d');
	    
	    //var source = "WebContent/score.jpg";
	    //loadImg(ctx, source,30,30);
	   // drawLine(ctx,1,1,40,40,"red");
		//drawRect(ctx,30,40,20,10,"");
    
	    allLine(ctx, Line);		//draw all lines
	    allCircleU(ctx, CircleU);	    //draw all unfilled circles
	    allBus(ctx, BusLine);			//draw all buses
	    
	    allCircle(ctx, Circle);		//draw all circles
	    allRect(ctx, Rect);			//draw all rectangles
	    try{
	    	allTri(ctx, Tri);			//draw all triangles
	    }catch(exception){
	    	alert("tri error!");
	    }
	    try{
		    allText(ctx, Text);		//draw all texts
	    }catch(exception){
	    	alert("text error!");
	    }

    }
    catch(exception){
        alert("Ordinate Data Error! Please check again!");
    }
};

function allBus(ctx, BusLine){
	if (BusLine.length!=0){
		for (var bus_i=0; bus_i<BusLine.length; bus_i++){
			// drawLine(ctx,1,1,40,40,"red");
		    drawLine(ctx, BusLine[bus_i][1], BusLine[bus_i][2], BusLine[bus_i][3], BusLine[bus_i][4], "lightskyblue", BUS_LINE_WIDTH);
		}
	}
}

function allLine(ctx, Line){
	if (Line.length!=0){
		for (var line_i=0; line_i<Line.length; line_i++){
			// drawLine(ctx,1,1,40,40,"red");
		    drawLine(ctx, Line[line_i][1], Line[line_i][2], Line[line_i][3], Line[line_i][4], "white", DEFAULT_LINE_WIDTH);
		}
	}
}

function allCircleU(ctx, CircleU){
	if (CircleU.length!=0){
		for (var circleu_i=0; circleu_i<CircleU.length; circleu_i++){
			    drawCircle(ctx, CircleU[circleu_i][1], CircleU[circleu_i][2], R_TRANSFORMER, "");
		}
	}
}

function allCircle(ctx, Circle){
	if (Circle.length!=0){
		var color="";
		for (var circle_i=0; circle_i<Circle.length; circle_i++){
			if (Circle[circle_i][3] == 0)
				color = "red";
			else
				color = "springgreen";
			drawCircle(ctx, Circle[circle_i][1], Circle[circle_i][2], R_DISCONNECTOR, color);
		}
	}	
}

function allRect(ctx, Rect){
	if (Rect.length!=0){
		var color="";
		for (var rect_i=0; rect_i<Rect.length; rect_i++){
			if (Rect[rect_i][3] == 0)
				color = "red";
			else
				color = "springgreen";
			drawRect(ctx, Rect[rect_i][1]-BREAKER_LENGTH/2, Rect[rect_i][2]-BREAKER_LENGTH/2, BREAKER_LENGTH, BREAKER_LENGTH, color);
		}
	}
}

function allTri(ctx, Tri){
	if (Tri.length!=0){
		for (var tri_i=0; tri_i<Tri.length; tri_i++)
			drawTri(ctx, Tri[tri_i][1], Tri[tri_i][2], TRI_BASE_LENGTH, TRI_HEIGHT);
	}
}

function allText(ctx, Text){
	if (Text.length!=0){
		for (var i=0; i < Text.length; i++)
			if (Text[i][0] == "Syn")
				setText(ctx, Text[i][1], Text[i][2]+R_TRANSFORMER/2, "G", G_FONTSIZE);
			else if (Text[i][0] == "Title")
				setText(ctx, Text[i][1], Text[i][2], Text[i][3], TITLE_FONTSIZE);
			else
				setText(ctx, Text[i][1], Text[i][2], Text[i][3], DEFAULT_FONTSIZE);
	}
}

function loadImg(ctx, source, x, y){
	var img=new Image();
	img.onload = function(){
	ctx.drawImage(img,x,y);
	};
	img.src=source;
}

function drawLine(ctx,x0,y0,x1,y1,color, width){
	ctx.beginPath();
	ctx.moveTo(x0,y0);
	ctx.lineTo(x1,y1);
	if (color != ""){
		ctx.strokeStyle = color;
	};
	ctx.lineWidth = width;
	ctx.stroke();
	ctx.closePath();
}

function drawCircle(ctx, x0, y0, r, fill_color){
	ctx.beginPath();
	ctx.arc(x0,y0,r,0,Math.PI*2,true);
	if (fill_color != ""){
		ctx.fillStyle=fill_color;
		ctx.fill();
	}
	else{
		ctx.lineWidth = DEFAULT_LINE_WIDTH;
		ctx.strokeStyle = "white";
		ctx.stroke();
	}	
	ctx.closePath();
}

function drawRect(ctx, x0, y0, a, b, color){
	if (color != ""){
		ctx.fillStyle=color;
	};
	ctx.fillRect(x0,y0,a,b);
}

//draw Triangle
function drawTri(ctx, x0, y0, base_length, height){
	ctx.beginPath();
	ctx.moveTo(x0, y0);
	ctx.lineTo(x0+base_length/2, y0);
	ctx.lineTo(x0, y0-height);
	ctx.lineTo(x0-base_length/2, y0);
	ctx.lineTo(x0,y0);
	ctx.closePath();
	ctx.fillStyle = "lightskyblue";
	ctx.fill();
}

//create Text
function setText(ctx, x0, y0, content, font_size){
	ctx.font = font_size.toString() + "px Arial";		//modify font size
	ctx.textAlign = "center";									//center
	ctx.fillStyle = "white";
	ctx.fillText(content, x0, y0);
}